$(document).ready(function(){
	var ctx = $("#ctx").val();
	var flow=[];
	var tipLabel=["Impression","Click","页面PV","页面UV","Income"];
	var pv=tipLabel[0];
	$.ajax({
		type : 'post',
		url :  '../main/dashboard/list',
		async : false,
		cache : false,
		dataType : 'json',
		success : function(data){
			var impressionArray = [];
			var clickArray = [];
			var pvArray = [];
			var uvArray = [];
			var incomeArray = [];
			$.each(data,function(n,value) {
				impressionArray[6-n] = value.impression;
				clickArray[6-n] = value.click;
				pvArray[6-n] = value.pv;
				uvArray[6-n] = value.uv;
				incomeArray[6-n] = value.income;
			});
			
		 	flow[0] = impressionArray;
			flow[1] = clickArray;
			flow[2] = pvArray;
			flow[3] = uvArray;
			flow[4] = incomeArray;
		},
		error : function (XMLHttpRequest, textStatus, errorThrown) {
			// 通常 textStatus 和 errorThrown 之中
			// 只有一个会包含信息
			alert('error');
			this; // 调用本次AJAX请求时传递的options参数
		}
	});
		
	//设置日期，当前日期的前七天
	var myDate = new Date(); //获取今天日期
	var myYear = myDate.getFullYear();//获取年份
	myDate.setDate(myDate.getDate() - 7);
	var labels = [];
	var dateTemp;
	var flag = 1;
	for (var i = 0; i < 7; i++) {
		dateTemp = myDate.getFullYear() + "/" + (myDate.getMonth() + 1) + "/" + myDate.getDate();
		labels.push(dateTemp);
		myDate.setDate(myDate.getDate() + flag);
	}		
	drawDiagram(myDate,flow[0],pv);
		
	$('#data_number td').click(function(){
		$('#data_number td').css({"background-color":"#ffffff","color":"black"});
		$(this).css({"background-color":"#36aeea","color":"#ffffff"});
		var n = $(this).index();
		pv=tipLabel[n];
		var data = flow[n];
		$('#baseTitle').text('近7天全部广告位' + pv);
		drawDiagram(myDate,data,pv);
	});
		
	function drawDiagram(myDate,data,pv){		
		$('#diagram').highcharts({ 
			chart: { type: 'line' },
			title: { text: '' }, //标题
			//subtitle: { text: 'Source: WorldClimate.com' }, //副标题
			xAxis: { categories: labels }, 
			yAxis: { 
				title: { text: '' }, 
				plotLines: [{ value: 0, width: 1, color: '#808080' }]
			}, 
			tooltip: { crosshairs: true, shared: true }, 
			legend: { 
				enabled:false,
			},
			credits:{
				enabled:false,
			},
			
			series: [{ 
				name: pv, 
				data: data,
				color:'#36aeea',
				marker:{
					symbol: 'circle',
					fillColor:'#ffffff',
					lineColor:'#87b9f6',
					lineWidth: 2,
					radius: 5,
				}
			}]
		}); 
	}
});